using System;
using System.Windows.Input;
using WpfMvvmAppWcf.Helper;
using WpfMvvmAppWcf.ServiceReference;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace WpfMvvmAppWcf.ViewModels
{
    public class Windows1ViewModel : BaseViewModel
    {
        private string _login;
        private string _password;
        private string _error;
        private Flux _selectedFlux;
        private Flux _RemoveFlux;
        private Flux _EditFlux;
        private string _rss;
        private LoginModel model;
        private ServiceRssClient client = null;
        private string _urlAdd;
        private string _titleAdd;
        private string _urlEdit;
        private string _titleEdit;
        private bool _isOpen;

        public ObservableCollection<Flux> Flux { get; private set; }
        public ICommand PersonSelected { get; private set; }

        public ICommand ClickCommand { get { return new BaseCommand(Click); } }
        public ICommand RemoveCommand { get { return new BaseCommand(RemoveFlux); } }
        public ICommand EditCommand { get { return new BaseCommand(FluxEdit); } }
        public ICommand ConfirmEditCommand { get { return new BaseCommand(ConfirmEdit); } }
        public ICommand AddCommand { get { return new BaseCommand(Add); } }

        public Windows1ViewModel()
        {
            Flux = new ObservableCollection<Flux>();
            
        }

        public void ConfirmEdit()
        {
            Flux selected = SelectedFlux;

            if (selected != null && _urlEdit != null && _titleEdit != null)
            {
                EditFlux editFlux = new EditFlux()
                {
                    ID = selected.ID,
                    Link = _urlEdit,
                    Title = _titleEdit
                };

                try
                {
                    this.client.EditRss(this.model, editFlux);
                    Flux[] tabFlux = this.client.GetFlux(this.model);

                    this.Flux.Clear();
                    foreach (Flux item in tabFlux)
                    {
                        this.Flux.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Serveur Exeption :" + ex.Message);
                }


            }
            OpenPopUp = false;
        }

        public void FluxEdit()
        {
            if (SelectedFlux != null)
            {
                OpenPopUp = true;
                UrlEdit = SelectedFlux.Link;
                TitleEdit = SelectedFlux.Title;
                if (_EditFlux == null)
                    _EditFlux = new Flux();
            }
        }

        public Flux SelectedFlux
        {
            get { return _selectedFlux; }
            set
            {
                _selectedFlux = value;
                OnPropertyChanged(() => SelectedFlux);
                if (_selectedFlux != null)
                {
                    GetRss();
                }
            }
        }

        public bool OpenPopUp
        {
            get { return _isOpen; }
            set
            {
                _isOpen = value;
                OnPropertyChanged(() => OpenPopUp);
            }
        }

        public void RemoveFlux()
        {
            Flux selected = SelectedFlux;

            if (selected != null)
            {
                DeleteRss delRss = new DeleteRss()
                {
                    IdFlux = selected.ID,
                    UserName = this.model.UserName
                };

                try
                {
                    this.client.RemoveRss(this.model, delRss);
                    Flux[] tabFlux = this.client.GetFlux(this.model);

                    this.Flux.Clear();
                    foreach (Flux item in tabFlux)
                    {
                        this.Flux.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Serveur Exeption :" + ex.Message);
                }
            }
        }

        public string UrlEdit
        {
            get { return _urlEdit; }
            set
            {
                _urlEdit = value;
                OnPropertyChanged(() => UrlEdit);
            }
        }

        public string TitleEdit
        {
            get { return _titleEdit; }
            set
            {
                _titleEdit = value;
                OnPropertyChanged(() => TitleEdit);
            }
        }

        public string Rss
        {
            get { return _rss; }
            set
            {
                _rss = value;
                OnPropertyChanged(() => Rss);
            }
        }

        private void GetRss()
        {
            try
            {
                Rss[] tabRss = this.client.GetRss(this.model, SelectedFlux);
                Rss = " ";
                foreach (var item in tabRss)
                {
                    Rss += item.Description + "<br/>";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serveur Exeption :" + ex.Message);
            }
        }

        private void Click()
        {
            this.model = new LoginModel() { Password = Password, UserName = Login };
            this.client = new ServiceRssClient();

            try
            {
                var connected = this.client.LogOn(model);
                if (connected == MembershipCreateStatus.Success)
                {
                    Error = connected.ToString();
                    Flux[] tabFlux = this.client.GetFlux(this.model);

                    this.Flux.Clear();
                    foreach (Flux item in tabFlux)
                    {
                        this.Flux.Add(item);
                    }
                }
                else
                    Error = connected.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serveur Exeption :" + ex.Message);
            }
        }

        public string UrlAdd
        {
            get { return _urlAdd; }
            set
            {
                _urlAdd = value;
                OnPropertyChanged(() => UrlAdd);
            }
        }

        public string TitleAdd
        {
            get { return _titleAdd; }
            set
            {
                _titleAdd = value;
                OnPropertyChanged(() => TitleAdd);
            }
        }

        private void Add()
        {
            if (UrlAdd != null && TitleAdd != null && this.client != null && this.model != null)
            {
                RegisterRss rss = new RegisterRss()
                {
                    Link = UrlAdd,
                    Title = TitleAdd,
                    UserName = this.model.UserName
                };

                try
                {
                    FluxStatus success = this.client.AddRss(this.model, rss);

                    if (success == FluxStatus.Success)
                    {
                        Flux[] tabFlux = this.client.GetFlux(this.model);

                        this.Flux.Clear();
                        foreach (Flux item in tabFlux)
                        {
                            this.Flux.Add(item);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Serveur Exeption :" + ex.Message);
                }
            }
        }

        public string Error
        {
            get { return _error; }
            set
            {
                _error = value;
                OnPropertyChanged(() => Error);
            }
        }

        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged(() => Password);
            }
        }

        public string Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged(() => Login);
            }
        }


    }


    public class BrowserBehavior
    {
        public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
                "Html",
                typeof(string),
                typeof(BrowserBehavior),
                new FrameworkPropertyMetadata(OnHtmlChanged));

        [AttachedPropertyBrowsableForType(typeof(WebBrowser))]
        public static string GetHtml(WebBrowser d)
        {
            return (string)d.GetValue(HtmlProperty);
        }

        public static void SetHtml(WebBrowser d, string value)
        {
            d.SetValue(HtmlProperty, value);
        }

        static void OnHtmlChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            WebBrowser webBrowser = dependencyObject as WebBrowser;
            if (webBrowser != null)
                webBrowser.NavigateToString(e.NewValue as string);
        }
    }

}